// 引入模块-------------
var express = require('express');
var path = require('path');


// 引入路由模块
var indexRouter = require('./routes/index');
var loginRegisterRouter = require('./routes/loginRegister');
var listRouter = require('./routes/list');
var usersRouter = require('./routes/users');

// 搭建服务器
var app = express();


// 设置ejs模块引擎 view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// 中间件-------------
// post中间件
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

// 静态资源中间件
app.use(express.static(path.join(__dirname, 'public')));
// 因为后期 各种文件（图片）上传，都会上传到uploads
app.use("/uploads",express.static(path.join(__dirname, 'uploads')));


// 注册路由中间件
app.use('/', indexRouter);
app.use("/",loginRegisterRouter)

app.use("/",listRouter)
app.use('/', usersRouter);


// 错误处理中间件
// catch 404 and forward to error handler
app.use(function (req, res, next) {
  res.send({
    status: 404,
    msg: "此路不通"
  })
});

// error handler
app.use(function (err, req, res, next) {
  res.send({
    status: 500,
    msg: err
  })
});

module.exports = app;
